﻿using famails.bus;
using famails.common;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.page
{
    public partial class Customer : System.Web.UI.Page
    {
        CustomerBUS ctBUS = new CustomerBUS();
        MailGroupBUS mgBUS = new MailGroupBUS();
        SendRegisterBUS srBUS = new SendRegisterBUS();
        DetailGroupBUS dtgBUS = new DetailGroupBUS();
        static log4net.ILog logs = log4net.LogManager.GetLogger("ErrorRollingLogFileAppender");
        static UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                try
                {
                    loadData();
                }
                catch (Exception)
                {
                }
            }

        }

        private void loadData()
        {
            try
            {
               
                DataTable MailGroup = new DataTable();
                if (getUserLogin().DepartmentId == 1)
                {
                    MailGroup = mgBUS.GetAllNew();
                }

                else
                {
                    MailGroup = mgBUS.GetMailGroupByUserId(getUserLogin().UserId);
                }

                DataRow dr = MailGroup.NewRow();
                dr["Name"] = "Tất cả";
                dr["Id"] = "-1";
                MailGroup.Rows.InsertAt(dr, 0);
                this.drlNhomMail.DataSource = MailGroup;
                this.drlNhomMail.DataTextField = "Name";
                this.drlNhomMail.DataValueField = "Id";
                this.drlNhomMail.DataBind();

                //pnSearch.Visible = true;
                //btnSearch.Visible = false;
            }
            catch (Exception ex)
            {

                logs.Error(userLogin.Username + "-Customer - LoadData", ex);

            }
        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            //else Response.Redirect("~");//test confict
            return null;
        }

      
        

        private void InitBUS()
        {
            ctBUS = new CustomerBUS();
            mgBUS = new MailGroupBUS();
            srBUS = new SendRegisterBUS();
            dtgBUS = new DetailGroupBUS();
        }
       

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<CustomerDTO> Search(string name, string phone, string email, int group, int status)
        {
            userLogin = (UserLoginDTO)HttpContext.Current.Session["us-login"];
            //string name = "", phone = "h", email = ""; int group = -1;
            CustomerBUS ctBUS = new CustomerBUS();
            DataTable customerBySelect = ctBUS.Search(userLogin.UserId, name, phone, email, group, status);
            List<CustomerDTO> result = new List<CustomerDTO>();
            CustomerDTO item = new CustomerDTO();
            foreach (DataRow dr in customerBySelect.Rows)
            {
                item = new CustomerDTO();
                item.Id = Convert.ToInt32(dr["id"] + "");
                item.Name = dr["Name"] + "";
                item.Email = dr["Email"] + "";
                item.Phone = dr["Phone"] + "";
                item.Job = dr["Job"] + "";
                item.Address = dr["Address"] + "";
                item.Company = dr["Company"] + "";
                if (dr["BirthDay"]+"" != "")
                    item.BirthDay = Convert.ToDateTime(dr["BirthDay"] + "");
                item.Status = Convert.ToBoolean(dr["recivedEmail"]) ? "Còn hoạt động" : "Mail chết";
                result.Add(item);
            }

            return result;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void DeleteEmail(decimal id)
        {
            try
            {
                CustomerBUS ctBUS = new CustomerBUS();
                DetailGroupBUS dtgBUS = new DetailGroupBUS();
                EventDetailBUS ebus = new EventDetailBUS();
                decimal CustomerID = id;
                ConnectionData.OpenMyConnection();
                ebus.tblEventDetail_DeleteEmailRegister(CustomerID, userLogin.UserId);
                ctBUS.tblCustomer_Delete(CustomerID);
                dtgBUS.tblDetailGroup_DeleteByCustomerID(CustomerID);
                ConnectionData.CloseMyConnection();
            }
            catch (Exception ex)
            {
                logs.Error("Customer - DeleteEmail", ex);
            }
        }
    }

}